<?php
class Control_DataAnalysis_goldSource {

	function goldSource() {
		$nowGame = $game 		= 	MooForm::request('game');
		$platform 	= 	MooForm::request('platform');
		$channel	= 	MooForm::request('channel');
        $server 	= MooForm::request('server');
		$startDate 	= 	MooForm::request('startTime');
		$endDate 	= 	MooForm::request('endTime');
		
		$game = MooObj::get('Game')->transportGame($game);
		$nowChannel = $channel = MooObj::get('Game')->transportChannel($game, $channel);
		
		if(!$startDate) {
			$startDate 	= 	date('Y-m-d', time());
		}
		if(!$endDate) {
			$endDate 	= 	date('Y-m-d', time());
		}
		$action 		= 	MooForm::request('action');
		MooView::set('action', $action);
		
		MooView::set('startDate', $startDate);
		MooView::set('endDate', $endDate);
		if($action == 0) {
			MooView::render('goldSource');
		} else if($action == 1) {
			MooObj::get('Control_Data')->setPlatformData($game);
			$urlConf = MooConfig::get('main.dataAnalysis.fetch_gold_source');
			$url = $urlConf . "&game=" . $game . "&beginDate=" . $startDate . "&endDate=" . $endDate;
			if ($platform) {
				$url = $url . "&platform=" . $platform;
			}
			if ($channel) {
				$url = $url . "&channel=" . $channel;
			}
            if($server) {
                MooView::set('server', $server);
                $url = $url . "&server=" . $server; //入参名???
            }
            $servers = MooObj::get('Gm')->getServerListByGame($game);
            MooView::set('servers', $servers);
			// $url = "http://182.254.230.39:9001/fetch_gold?game=wow&changetype=1&beginDate=2015-02-28&endDate=2015-03-31&platform=appstore&channel=appstore";
			
			 $data = MooUtil::curl_send($url, null, 30);
			// $data = '[{"changeaction":"ACC_RECHARGE2_AWARDS","count":"4","gold":"20000"},{"changeaction":"ACC_RECHARGE_AWARDS","count":"6469","gold":"1866240"},{"changeaction":"ACTIVE_REWARD","count":"136247","gold":"4438870"},{"changeaction":"CRAZY_ROULETTE","count":"3815","gold":"1265200"},{"changeaction":"DOWNLOAD_REWARD","count":"15971","gold":"1597100"},{"changeaction":"EMAIL_READ","count":"55377","gold":"19733650"},{"changeaction":"EVERYDAY_SIGN_REWARD","count":"34640","gold":"2798116"},{"changeaction":"FORTUNE","count":"636","gold":"353092"},{"changeaction":"GET_GROW_FUND_AWARD","count":"6622","gold":"6686072"},{"changeaction":"GM_AWARD","count":"2","gold":"2000000"},{"changeaction":"GROW_GIFT_BONUS","count":"532770","gold":"27682035"},{"changeaction":"LIFE_CARD_REWARD","count":"5868","gold":"586800"},{"changeaction":"MAIL_REWARD_TOOLS","count":"76019","gold":"10347928"},{"changeaction":"MONTH_CARD_REWARD","count":"11677","gold":"1167700"},{"changeaction":"OPEN_TREASURE","count":"71910","gold":"1840140"},{"changeaction":"RECHARGE","count":"24029","gold":"27153060"},{"changeaction":"SERVER_RECHARGE_WELFARE","count":"2500","gold":"287250"},{"changeaction":"SINGLE_RECHARGE","count":"765","gold":"449544"},{"changeaction":"START_ADVENTURE","count":"628008","gold":"5523295"},{"changeaction":"SYS_GIFT_DRAW","count":"40407","gold":"4703450"},{"changeaction":"USE_CDK","count":"16659","gold":"2221386"},{"changeaction":"WORDS_EXCHANGE_SPECIAL","count":"1680","gold":"624300"}]';
			
			$dataArr = MooJson::decode($data);
			MooArray::tdSort($dataArr, "gold", SORT_DESC);
			$actionConf = MooConfig::get('action_name.action');
			$totalGolds = 0;
			$goldRates = array();
			if($dataArr) {
				foreach ($dataArr as $key => $val) {
					$totalGolds += $val['gold'];
				}
					
				foreach ($dataArr as $k => $v) {
					if ($actionConf[$v['changeaction']]) {
						$dataArr[$k]['changeaction'] = $actionConf[$v['changeaction']];
					}
					$dataArr[$k]['rateNum'] = MooUtil::getPercent($v['gold'], $totalGolds, true); // 加true 不要百分号
					$dataArr[$k]['rate'] = MooUtil::getPercent($v['gold'], $totalGolds);
				}
					
				foreach ($dataArr as $k => $v) {
					//$goldRates[$v['changeaction']] = $v['rateNum'];
			
					$goldRates[$k][0]= $v['changeaction'];
					$goldRates[$k][1]= $v['rateNum'];
				}
			}
			
			$viewData = MooJson::encode($goldRates);
			
			// 格式化数据
			$viewData = str_replace(',"',',',$viewData);
			$viewData = str_replace('"]',']',$viewData);
			
			if($platform) {
				MooView::set('platform', $platform);
			}
			if ($channel) {
				MooView::set('channel', $nowChannel);
			}
			
			MooView::set('nowGame', $nowGame);
			MooView::set('viewData', $viewData);
			MooView::set('goldSource', $dataArr);
			MooView::render('goldSource');
		} else {
			exit("error!");
		}
	}
}